package com.example.service;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Help;
import com.example.entity.Helper;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.HelpMapper;
import com.example.mapper.HelperMapper;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 城市组织信息业务层
 */
@Service
public class HelperService {
    @Resource
    private HelperMapper helperMapper;
    @Resource
    private HelpMapper helpMapper;
    @Resource
    private UserMapper userMapper;

    public void add(Help help) {
        //进行选择援助逻辑处理
        //1.先后先判断有无满员
        if (help.getNum().equals(help.getAlreadyNum())){
            throw new CustomException("本援助已经满员，请选择其他援助方案");
        }
        //2判断是否重复选择
        List<Helper> list=helperMapper.selectByHelpIdAndUserId(help.getId(),help.getUserId());
        if(CollectionUtil.isNotEmpty(list)){
            throw new CustomException("你已经选择拯救这只宠物了！！！");
        }
        //3.开始插入选择记录
        Helper helper = new Helper();
        helper.setName(help.getName());
        helper.setVolunteerId(help.getVolunteerId());
        helper.setUserId(help.getUserId());
        helper.setHelpId(help.getId());
        helperMapper.insert(helper);
        //4.该救援已经选择的人数+1
        help.setAlreadyNum(help.getAlreadyNum()+1);
        helpMapper.updateById(help);

        //5.用户积分增加
        User user = userMapper.selectById(helper.getUserId());
        user.setIntegral(help.getScore() +user.getIntegral());
        userMapper.updateById(user);
        }

    public PageInfo<Helper> selectPage(Helper helper, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Helper> list;
        if (ObjectUtil.isNotEmpty(helper.getUserId())) {
            // 说明这是学生登录进行分页查询
            if (ObjectUtil.isNotEmpty(helper.getName())) {
                list = helperMapper.selectByNameAndUserId(helper.getName(), helper.getUserId());
            } else {
                list = helperMapper.selectAllByUserId(helper.getUserId());
            }
        } else if (ObjectUtil.isNotEmpty(helper.getVolunteerId())) {
            // 说明这是教师登录进行分页查询
            if (ObjectUtil.isNotEmpty(helper.getName())) {
                list = helperMapper.selectByNameAndVolunteerId(helper.getName(), helper.getVolunteerId());
            } else {
                list = helperMapper.selectAllByVolunteerId(helper.getVolunteerId());
            }
        } else {
            // 说明这是管理员登录进行分页查询
            if (ObjectUtil.isNotEmpty(helper.getName())) {
                list = helperMapper.selectByName(helper.getName());
            } else {
                list = helperMapper.selectAll();
            }
        }
        return PageInfo.of(list);
    }





    public void updateById(Helper helper) {
        helperMapper.updateById(helper);
    }

    public void deleteById(Integer id) {
        Helper helper = helperMapper.selecctById(id);
        helperMapper.deleteById(id);
        //把援助信息-1
        Help help = helpMapper.selectById(helper.getHelpId());
        help.setAlreadyNum(help.getAlreadyNum() - 1);
        helpMapper.updateById(help);
        //把积分剪掉
        Help dbHelp = helpMapper.selectById(helper.getHelpId());
        User user = userMapper.selectById(helper.getUserId());
        user.setIntegral(user.getIntegral()- dbHelp.getScore());
        userMapper.updateById(user);
    }

    public List<Helper> selectAll() {
        return helperMapper.selectAll();
    }
}

